clear; clc;
[device, props] = ziCreateAPISession('dev3546', 6);
ziDAQ('sync');

h = ziDAQ('dataAcquisitionModule');

ziDAQ('set', h, 'dataAcquisitionModule/type', 0);
ziDAQ('set', h, 'dataAcquisitionModule/preview', 1);
ziDAQ('set', h, 'dataAcquisitionModule/grid/waterfall', 1);
ziDAQ('set', h, 'dataAcquisitionModule/grid/overwrite', 1);
ziDAQ('set', h, 'dataAcquisitionModule/device', 'dev3546');
ziDAQ('set', h, 'dataAcquisitionModule/historylength', 10);
ziDAQ('set', h, 'dataAcquisitionModule/delay', 0.0000000);
ziDAQ('set', h, 'dataAcquisitionModule/spectrum/enable', 1);
ziDAQ('set', h, 'dataAcquisitionModule/grid/rows', 1);
ziDAQ('set', h, 'dataAcquisitionModule/grid/repetitions', 10);
ziDAQ('set', h, 'dataAcquisitionModule/grid/cols', 8192);
ziDAQ('set', h, 'dataAcquisitionModule/endless', 0);
ziDAQ('set', h, 'dataAcquisitionModule/spectrum/frequencyspan', 400.0000000);
ziDAQ('set', h, 'dataAcquisitionModule/fft/window', 0);

ziDAQ('subscribe', h, '/dev3546/demods/0/sample.X.fft.abs.avg');
ziDAQ('execute', h);

while ~ziDAQ('finished', h)
  pause(1);
  result = ziDAQ('read', h);
  fprintf('Progress %0.0f%%\n', ziDAQ('progress', h) * 100)
  if ziCheckPathInData(result, '/dev3546/demods/0/sample_x_fft_abs_avg')
      fprintf('asdf\n');
    plotData(result.dev3546.demods(1).sample_x_fft_abs_avg{1});
  end
end

ziDAQ('finish', h);
ziDAQ('unsubscribe', h, '*');


function plotData(sample)
    freq=double(0:(sample.header.gridcols-1))*sample.header.gridcoldelta;
    val=sample.value;
    semilogy(freq, val);
end
